<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>inputdatepicker.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_inputdatepicker.js.html">inputdatepicker.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Input Date Picker implementation.  Pairs a
<a name="line17"></a> * goog.ui.PopupDatePicker with an input element and handles the input from
<a name="line18"></a> * either.
<a name="line19"></a> *
<a name="line20"></a> * @see ../demos/inputdatepicker.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.ui.InputDatePicker&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.date.DateTime&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom&#39;);
<a name="line27"></a>goog.require(&#39;goog.string&#39;);
<a name="line28"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line29"></a>goog.require(&#39;goog.ui.DatePicker&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.PopupBase&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.PopupDatePicker&#39;);
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>/**
<a name="line36"></a> * Input date picker widget.
<a name="line37"></a> *
<a name="line38"></a> * @param {goog.i18n.DateTimeFormat} dateTimeFormatter A formatter instance
<a name="line39"></a> *     used to format the date picker&#39;s date for display in the input element.
<a name="line40"></a> * @param {goog.i18n.DateTimeParse} dateTimeParser A parser instance used to
<a name="line41"></a> *     parse the input element&#39;s string as a date to set the picker.
<a name="line42"></a> * @param {goog.ui.DatePicker=} opt_datePicker Optional DatePicker.  This
<a name="line43"></a> *     enables the use of a custom date-picker instance.
<a name="line44"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
<a name="line45"></a> * @extends {goog.ui.Component}
<a name="line46"></a> * @constructor
<a name="line47"></a> */
<a name="line48"></a>goog.ui.InputDatePicker = function(
<a name="line49"></a>    dateTimeFormatter, dateTimeParser, opt_datePicker, opt_domHelper) {
<a name="line50"></a>  goog.ui.Component.call(this, opt_domHelper);
<a name="line51"></a>
<a name="line52"></a>  this.dateTimeFormatter_ = dateTimeFormatter;
<a name="line53"></a>  this.dateTimeParser_ = dateTimeParser;
<a name="line54"></a>
<a name="line55"></a>  this.popupDatePicker_ = new goog.ui.PopupDatePicker(
<a name="line56"></a>      opt_datePicker, opt_domHelper);
<a name="line57"></a>  this.addChild(this.popupDatePicker_);
<a name="line58"></a>  this.popupDatePicker_.setAllowAutoFocus(false);
<a name="line59"></a>};
<a name="line60"></a>goog.inherits(goog.ui.InputDatePicker, goog.ui.Component);
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Used to format the date picker&#39;s date for display in the input element.
<a name="line65"></a> * @type {goog.i18n.DateTimeFormat}
<a name="line66"></a> * @private
<a name="line67"></a> */
<a name="line68"></a>goog.ui.InputDatePicker.prototype.dateTimeFormatter_ = null;
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Used to parse the input element&#39;s string as a date to set the picker.
<a name="line73"></a> * @type {goog.i18n.DateTimeParse}
<a name="line74"></a> * @private
<a name="line75"></a> */
<a name="line76"></a>goog.ui.InputDatePicker.prototype.dateTimeParser_ = null;
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * The instance of goog.ui.PopupDatePicker used to pop up and select the date.
<a name="line81"></a> * @type {goog.ui.PopupDatePicker}
<a name="line82"></a> * @private
<a name="line83"></a> */
<a name="line84"></a>goog.ui.InputDatePicker.prototype.popupDatePicker_ = null;
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * The element that the PopupDatePicker should be parented to. Defaults to the
<a name="line89"></a> * body element of the page.
<a name="line90"></a> * @type {Element}
<a name="line91"></a> * @private
<a name="line92"></a> */
<a name="line93"></a>goog.ui.InputDatePicker.prototype.popupParentElement_ = null;
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Returns the PopupDatePicker&#39;s internal DatePicker instance.  This can be
<a name="line98"></a> * used to customize the date picker&#39;s styling.
<a name="line99"></a> *
<a name="line100"></a> * @return {goog.ui.DatePicker} The internal DatePicker instance.
<a name="line101"></a> */
<a name="line102"></a>goog.ui.InputDatePicker.prototype.getDatePicker = function() {
<a name="line103"></a>  return this.popupDatePicker_.getDatePicker();
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * Returns the selected date, if any.  Compares the dates from the date picker
<a name="line109"></a> * and the input field, causing them to be synced if different.
<a name="line110"></a> * @return {goog.date.Date?} The selected date, if any.
<a name="line111"></a> */
<a name="line112"></a>goog.ui.InputDatePicker.prototype.getDate = function() {
<a name="line113"></a>
<a name="line114"></a>  // The user expectation is that the date be whatever the input shows.
<a name="line115"></a>  // This method biases towards the input value to conform to that expectation.
<a name="line116"></a>
<a name="line117"></a>  var inputDate = this.getInputValueAsDate_();
<a name="line118"></a>  var pickerDate = this.popupDatePicker_.getDate();
<a name="line119"></a>
<a name="line120"></a>  if (inputDate &amp;&amp; pickerDate) {
<a name="line121"></a>    if (!inputDate.equals(pickerDate)) {
<a name="line122"></a>      this.popupDatePicker_.setDate(inputDate);
<a name="line123"></a>    }
<a name="line124"></a>  } else {
<a name="line125"></a>    this.popupDatePicker_.setDate(null);
<a name="line126"></a>  }
<a name="line127"></a>
<a name="line128"></a>  return inputDate;
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Sets the selected date.  See goog.ui.PopupDatePicker.setDate().
<a name="line134"></a> * @param {goog.date.Date?} date The date to set.
<a name="line135"></a> */
<a name="line136"></a>goog.ui.InputDatePicker.prototype.setDate = function(date) {
<a name="line137"></a>  this.popupDatePicker_.setDate(date);
<a name="line138"></a>};
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Sets the value of the input element.  This can be overridden to support
<a name="line143"></a> * alternative types of input setting.
<a name="line144"></a> *
<a name="line145"></a> * @param {string} value The value to set.
<a name="line146"></a> */
<a name="line147"></a>goog.ui.InputDatePicker.prototype.setInputValue = function(value) {
<a name="line148"></a>  var el = this.getElement();
<a name="line149"></a>  if (el.labelInput_) {
<a name="line150"></a>    var labelInput = /** @type {goog.ui.LabelInput} */ (el.labelInput_);
<a name="line151"></a>    labelInput.setValue(value);
<a name="line152"></a>  } else {
<a name="line153"></a>    el.value = value;
<a name="line154"></a>  }
<a name="line155"></a>};
<a name="line156"></a>
<a name="line157"></a>
<a name="line158"></a>/**
<a name="line159"></a> * Returns the value of the input element.  This can be overridden to support
<a name="line160"></a> * alternative types of input getting.
<a name="line161"></a> *
<a name="line162"></a> * @return {string} The input value.
<a name="line163"></a> */
<a name="line164"></a>goog.ui.InputDatePicker.prototype.getInputValue = function() {
<a name="line165"></a>  var el = this.getElement();
<a name="line166"></a>  if (el.labelInput_) {
<a name="line167"></a>    var labelInput = /** @type {goog.ui.LabelInput} */ (el.labelInput_);
<a name="line168"></a>    return labelInput.getValue();
<a name="line169"></a>  } else {
<a name="line170"></a>    return el.value;
<a name="line171"></a>  }
<a name="line172"></a>};
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Gets the input element value and attempts to parse it as a date.
<a name="line177"></a> *
<a name="line178"></a> * @return {goog.date.Date?} The date object is returned if the parse
<a name="line179"></a> *      is successful, null is returned on failure.
<a name="line180"></a> * @private
<a name="line181"></a> */
<a name="line182"></a>goog.ui.InputDatePicker.prototype.getInputValueAsDate_ = function() {
<a name="line183"></a>  var value = goog.string.trim(this.getInputValue());
<a name="line184"></a>  if (value) {
<a name="line185"></a>    var date = new goog.date.DateTime();
<a name="line186"></a>    // DateTime needed as parse assumes it can call getHours(), getMinutes(),
<a name="line187"></a>    // etc, on the date if hours and minutes aren&#39;t defined.
<a name="line188"></a>    if (this.dateTimeParser_.strictParse(value, date) &gt; 0) {
<a name="line189"></a>      return date;
<a name="line190"></a>    }
<a name="line191"></a>  }
<a name="line192"></a>
<a name="line193"></a>  return null;
<a name="line194"></a>};
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * Creates an input element for use with the popup date picker.
<a name="line199"></a> * @override
<a name="line200"></a> */
<a name="line201"></a>goog.ui.InputDatePicker.prototype.createDom = function() {
<a name="line202"></a>  this.setElementInternal(
<a name="line203"></a>      this.getDomHelper().createDom(&#39;input&#39;, {&#39;type&#39;: &#39;text&#39;}));
<a name="line204"></a>  this.popupDatePicker_.createDom();
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Sets the element that the PopupDatePicker should be parented to. If not set,
<a name="line210"></a> * defaults to the body element of the page.
<a name="line211"></a> * @param {Element} el The element that the PopupDatePicker should be parented
<a name="line212"></a> *     to.
<a name="line213"></a> */
<a name="line214"></a>goog.ui.InputDatePicker.prototype.setPopupParentElement = function(el) {
<a name="line215"></a>  this.popupParentElement_ = el;
<a name="line216"></a>};
<a name="line217"></a>
<a name="line218"></a>
<a name="line219"></a>/** @override */
<a name="line220"></a>goog.ui.InputDatePicker.prototype.enterDocument = function() {
<a name="line221"></a>  goog.ui.InputDatePicker.superClass_.enterDocument.call(this);
<a name="line222"></a>  var el = this.getElement();
<a name="line223"></a>
<a name="line224"></a>  (this.popupParentElement_ || this.getDomHelper().getDocument().body).
<a name="line225"></a>      appendChild(this.popupDatePicker_.getElement());
<a name="line226"></a>  this.popupDatePicker_.enterDocument();
<a name="line227"></a>  this.popupDatePicker_.attach(el);
<a name="line228"></a>
<a name="line229"></a>  // Set the date picker to have the input&#39;s initial value, if any.
<a name="line230"></a>  this.popupDatePicker_.setDate(this.getInputValueAsDate_());
<a name="line231"></a>
<a name="line232"></a>  var handler = this.getHandler();
<a name="line233"></a>  handler.listen(this.popupDatePicker_, goog.ui.DatePicker.Events.CHANGE,
<a name="line234"></a>                 this.onDateChanged_);
<a name="line235"></a>  handler.listen(this.popupDatePicker_, goog.ui.PopupBase.EventType.SHOW,
<a name="line236"></a>                 this.onPopup_);
<a name="line237"></a>};
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/** @override */
<a name="line241"></a>goog.ui.InputDatePicker.prototype.exitDocument = function() {
<a name="line242"></a>  goog.ui.InputDatePicker.superClass_.exitDocument.call(this);
<a name="line243"></a>  var el = this.getElement();
<a name="line244"></a>
<a name="line245"></a>  this.popupDatePicker_.detach(el);
<a name="line246"></a>  this.popupDatePicker_.exitDocument();
<a name="line247"></a>  goog.dom.removeNode(this.popupDatePicker_.getElement());
<a name="line248"></a>};
<a name="line249"></a>
<a name="line250"></a>
<a name="line251"></a>/** @override */
<a name="line252"></a>goog.ui.InputDatePicker.prototype.decorateInternal = function(element) {
<a name="line253"></a>  goog.ui.InputDatePicker.superClass_.decorateInternal.call(this, element);
<a name="line254"></a>
<a name="line255"></a>  this.popupDatePicker_.createDom();
<a name="line256"></a>};
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>/** @override */
<a name="line260"></a>goog.ui.InputDatePicker.prototype.disposeInternal = function() {
<a name="line261"></a>  goog.ui.InputDatePicker.superClass_.disposeInternal.call(this);
<a name="line262"></a>  this.popupDatePicker_.dispose();
<a name="line263"></a>  this.popupDatePicker_ = null;
<a name="line264"></a>  this.popupParentElement_ = null;
<a name="line265"></a>};
<a name="line266"></a>
<a name="line267"></a>
<a name="line268"></a>/**
<a name="line269"></a> * See goog.ui.PopupDatePicker.showPopup().
<a name="line270"></a> * @param {Element} element Reference element for displaying the popup -- popup
<a name="line271"></a> *     will appear at the bottom-left corner of this element.
<a name="line272"></a> */
<a name="line273"></a>goog.ui.InputDatePicker.prototype.showForElement = function(element) {
<a name="line274"></a>  this.popupDatePicker_.showPopup(element);
<a name="line275"></a>};
<a name="line276"></a>
<a name="line277"></a>
<a name="line278"></a>/**
<a name="line279"></a> * See goog.ui.PopupDatePicker.hidePopup().
<a name="line280"></a> */
<a name="line281"></a>goog.ui.InputDatePicker.prototype.hidePopup = function() {
<a name="line282"></a>  this.popupDatePicker_.hidePopup();
<a name="line283"></a>};
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>/**
<a name="line287"></a> * Event handler for popup date picker popup events.
<a name="line288"></a> *
<a name="line289"></a> * @param {goog.events.Event} e popup event.
<a name="line290"></a> * @private
<a name="line291"></a> */
<a name="line292"></a>goog.ui.InputDatePicker.prototype.onPopup_ = function(e) {
<a name="line293"></a>  this.setDate(this.getInputValueAsDate_());
<a name="line294"></a>};
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * Event handler for date change events.  Called when the date changes.
<a name="line299"></a> *
<a name="line300"></a> * @param {goog.ui.DatePickerEvent} e Date change event.
<a name="line301"></a> * @private
<a name="line302"></a> */
<a name="line303"></a>goog.ui.InputDatePicker.prototype.onDateChanged_ = function(e) {
<a name="line304"></a>  this.setInputValue(e.date ? this.dateTimeFormatter_.format(e.date) : &#39;&#39;);
<a name="line305"></a>};
</pre>


</body>
</html>
